import {Component, OnInit, ViewChild} from '@angular/core';
import {BaseGridComponent, BaseService} from '@sb/base';
import {OuOrgSearchConfig} from '../entity/ou-org-search-config';
import {OuOrgUserService} from '../service/ou-org-user.service';
import {OuOrgUserSearchForm} from '../entity/ou-org-user-search-form';
import {OuOrgUserTreeSelectPanelComponent} from '../../ou-org-user-tree-select-panel/ou-org-user-tree-select-panel.component';
import {NzTreeNode} from 'ng-zorro-antd/core/tree/nz-tree-base-node';

@Component({
    selector: 'ou-org-user',
    templateUrl: './ou-org-user.component.html',
    styleUrls: ['./ou-org-user.component.less'],
    providers: [
        {
            provide: BaseService,
            useClass: OuOrgUserService
        }
    ]
})
export class OuOrgUserComponent implements OnInit {

    activeNode: NzTreeNode;

    @ViewChild(OuOrgUserTreeSelectPanelComponent, {static: true})
    ouOrgUserTreeSelectPanel: OuOrgUserTreeSelectPanelComponent;

    baseParam: OuOrgUserSearchForm = {
        orgId: '-1'
    };

    searchConfig: OuOrgSearchConfig = {
        searchSubOrg: true,
        searchDept: true,
        searchUser: false,
    };

    settingOptions = [
        {label: '子机构', value: 'subOrg', checked: true},
        {label: '部门', value: 'dept', checked: true},
        {label: '用户', value: 'user'}
    ];

    @ViewChild('baseGrid', {static: false})
    baseGrid: BaseGridComponent;

    constructor() {
    }

    ngOnInit(): void {
    }

    selectNode(selectedNodeTreeNode: NzTreeNode): void {
        this.activeNode = selectedNodeTreeNode;
    }

    settingChange(value: any[]) {
        value.forEach(item => {
            if (item.value === 'subOrg') {
                this.searchConfig.searchSubOrg = item.checked;
            } else if (item.value === 'dept') {
                this.searchConfig.searchDept = item.checked;
            } else if (item.value === 'user') {
                this.searchConfig.searchUser = item.checked;
            }
        });
        this.ouOrgUserTreeSelectPanel.reload().subscribe(res => {
            const node = this.ouOrgUserTreeSelectPanel.baseTree.nzTree.getTreeNodeByKey(this.activeNode.key);
            if (node === null) {
                this.ouOrgUserTreeSelectPanel.baseTree.triggerClick('-1');
            }
        });
    }

    dataChange(type) {
        if (type === 'detail') {
            if (this.activeNode.parentNode) {
                this.ouOrgUserTreeSelectPanel.reloadNode(this.activeNode.parentNode.key);
            } else {
                this.ouOrgUserTreeSelectPanel.baseTree.reloadNode('-1', true);
            }
        } else if (type === 'user') {
            if (this.searchConfig.searchUser) {
                this.ouOrgUserTreeSelectPanel.reloadNode(this.activeNode.key);
            }
        } else if (type === 'org') {
            this.ouOrgUserTreeSelectPanel.reloadNode(this.activeNode.key);
        }
    }

    itemClick(key) {
        this.ouOrgUserTreeSelectPanel.baseTree.triggerClick(key);
    }

}
